'''COPYRIGHT NOTICE
THIS PROGRAM IS COPYRIGHT 2013 MICHAEL STAGGS
THE COPYRIGHT HOLDER CAN BE REACHED AT TAUSCIAM@GMAIL.COM

    This file is part of Kids' Media Player.

    Kids' Media Player is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Kids' Media Player is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Kids' Media Player.  If not, see <http://www.gnu.org/licenses/>.
'''

import sip
sip.setapi('QString',2) 

from PyQt4.QtCore import *
from PyQt4.QtGui import *
import os
import json

def getMusicInfo():
  
    musicPath = QDesktopServices.storageLocation(QDesktopServices.MusicLocation)
    musicdict = {}
    for (path, dirs, files) in os.walk(musicPath):
        for thisdir in dirs:
            if thisdir.startswith("."):
                dirs.remove(thisdir)
        if dirs == []:
            biglist = []
            head,album = os.path.split(path)
            head2,artist = os.path.split(head)
            musiclist = []
            coverartpath = ""
            for file in files:
                if file.lower().endswith("mp3"):
                    file.encode('utf8')
                    musiclist.append(path + "/" + file)
                else:
                    if file.lower().endswith("jpg"):
                        coverartpath = path + "/" + file
            
            biglist.append(musiclist)
            biglist.append(coverartpath)
            if musicdict.has_key(album):
                templist = musicdict[album]
                tempmusic = templist[0]
                for item in musiclist:
                    tempmusic.append(item)
                templist[0] = tempmusic
                musicdict.update({album : templist})
            else:
                musicdict.update({album : biglist})
                
    json.dump(musicdict, open("music.dir", "w"))
